package com.qing.expert.modules.notification.controller;

import com.qing.expert.common.response.R;
import com.qing.expert.modules.notification.service.NotificationService;
import com.qing.expert.modules.notification.vo.response.NotificationVO;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/notifications")
public class NotificationController {

    @Resource
    private NotificationService notificationService;

    /**
     * 获取通知列表
     */
    @GetMapping
    public R<List<NotificationVO>> getNotificationList(HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        return R.ok(notificationService.getNotificationList(userId));
    }

    /**
     * 标记通知为已读
     */
    @PutMapping("/{notificationId}/read")
    public R<Void> markAsRead(
            @PathVariable String notificationId,
            HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        notificationService.markAsRead(userId, notificationId);
        return R.ok();
    }

    /**
     * 标记所有通知为已读
     */
    @PutMapping("/read/all")
    public R<Void> markAllRead(HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        notificationService.markAllRead(userId);
        return R.ok();
    }

    /**
     * 获取未读通知数量
     */
    @GetMapping("/unread/count")
    public R<Integer> getUnreadCount(HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        return R.ok(notificationService.getUnreadCount(userId));
    }
}